package de.argh.codebook.controller;

import java.beans.PropertyEditorSupport;
import java.security.Principal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.validation.Valid;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import de.argh.codebook.model.Article;
import de.argh.codebook.model.Coder;
import de.argh.codebook.model.PageState;
import de.argh.codebook.model.ValidationResponse;
import de.argh.codebook.service.ArticleService;
import de.argh.codebook.service.CoderService;

@Secured({ "ROLE_USER" })
@Controller
public class ArticleController {

  @Inject
  private ArticleService articleService;

  @Inject
  private CoderService coderService;

  @Inject
  private ValidationHelper validationHelper;

  @InitBinder
  public void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));

    binder.registerCustomEditor(Coder.class, new PropertyEditorSupport() {
      @Override
      public void setAsText(String text) throws IllegalArgumentException {
        Coder author = coderService.getCoderById(Long.parseLong(text));
        super.setValue(author);
      }
    });
  }

  @RequestMapping(value = "/", method = RequestMethod.GET)
  public String home(Model model, Principal principal) {
    Coder coder = coderService.getCoderByPrincipalName(principal.getName());
    coder.setFollower(coderService.getFollower(coder.getId()));
    List<Article> articles = articleService.getArticelOverview(coder.getId(), new PageState(Long.valueOf(0), Long.valueOf(5)));

    model.addAttribute("coder", coder);
    model.addAttribute("articles", articles);

    return "home";
  }

  @RequestMapping(value = "/article/{id}", method = RequestMethod.GET)
  public String showArticle(Model model, @PathVariable("id") Long id) {
    Article article = articleService.getArticleById(id);
    article.setComments(articleService.getCommentsForArticle(id));

    model.addAttribute("article", article);
    return "article";
  }

  @RequestMapping(value = "/article/{id}", method = RequestMethod.DELETE)
  public String deleteArticle(@PathVariable("id") Long id) {
    articleService.deleteArticle(id);
    return "home";
  }

  @RequestMapping(value = "/article/validate.json", method = RequestMethod.POST)
  public @ResponseBody
  ValidationResponse processFormAjaxJson(Model model, @ModelAttribute(value = "articel") @Valid Article article, BindingResult result) {
    return validationHelper.validateBindingResult(result);
  }

}
